package logic

import (
	"context"
	"encoding/json"
	"time"
	"yuni/model/cache"
	"yuni/model/mysql"

	"yuni/yunirpc/internal/svc"
	"yuni/yunirpc/yunirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type EvaluateListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewEvaluateListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EvaluateListLogic {
	return &EvaluateListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *EvaluateListLogic) EvaluateList(in *yunirpc.EvaluateListRequest) (*yunirpc.EvaluateListResponse, error) {
	// todo: add your logic here and delete this line

	var m []mysql.Evaluate
	result, _ := cache.Rdb.Get(l.ctx, "Evaluate").Result()
	if result == "" {
		mysql.Db.Debug().Model(&mysql.Product{}).
			Joins("left join users on products.user_id = products.id").
			Joins("left join classes on products.class_id = classes.id").
			Find(&m)

		marshal, _ := json.Marshal(&m)
		cache.Rdb.Set(l.ctx, "Evaluate", marshal, time.Minute*15)

	} else {
		json.Unmarshal([]byte(result), &m)
	}
	var Product mysql.Evaluate
	mysql.Db.Debug().Where("id=?", in.ProductId).Find(&Product)

	var an []*yunirpc.EvaluateList
	for _, i2 := range m {
		list := yunirpc.EvaluateList{
			ProductId: i2.ProductId,
			Content:   i2.Content,
		}
		an = append(an, &list)
	}

	return &yunirpc.EvaluateListResponse{
		Title:   "火腿",
		Price:   1,
		Stock:   100,
		UserId:  1,
		ClassId: 1,
		List:    nil,
	}, nil
}
